var text = [{
  receiver: '执行人: ', publisher: '发布人: ',
  blank0: '暂无', blank1: '记录', tab1: '我申请的', tab2: '我发布的',
  ongoing: '进行中', tag_concluding: '结题中', tag_newTip: '有结题申请',
  load: '玩命加载中', end: '没有更多了哦', 
  title: '正在进行的项目', button: '发布需求', endTime: '截止',
}, {
  receiver: '执行人：', publisher: '发布人',
  blank0: '暂无', blank1: '记录', tab1: '我申请的', tab2: '我发布的',
  ongoing: '进行中', tag_concluding: '结题中', tag_newTip: '有结题申请',
  load: '玩命加载中', end: '没有更多了哦',
  title: '正在进行的项目', button: '发布需求', endTime: '截止',
}];
Page({

  onLoad(e) {
    this.setData({
      showWhat: e.release ? 1 : 0,
      text: text[wx.lang],
      theme: wx.theme,
    });
  },

  data: {
    blankIcon: "/assets/module/blank.png",
    defultAvatar: "/assets/logo/logo.jpg",
    showMask: true,
    P0: [],
    P1: [],
  },

  cardTap(e) {
    wx.showLoading({ title: text[wx.lang].load })
    var target = this.data[`P${e.currentTarget.id.split('-')[0]}`],
    index = e.currentTarget.id.split('-')[1];
    for (let j = 0; j < target.length; ++j) {
      if (index == target[j].idpractice) {
        // wx.post('Practice.getOngoingList', { idUser: wx.appData.user.iduser, tab: 0 }, res => {
        //   if (res.success) {
        //     for (let i = 0; i < res.data.length; ++i) 
        //       res.data[i].cardId = i;
        //     target[j].sign_up = res.data;
        //   } else {
        //     target[j].sign_up = [];
        //   }
          let string = JSON.stringify(target[j]);
          let path = e.currentTarget.id.split('-')[0] == 0 ? 'r_detail' : 'p_detail';
          wx.onHide(this, `../detail/${path}?content=${encodeURIComponent(string)}`);
        //   wx.hideLoading();
        // });
        break;
      }
    }
  },

  // 前端处理返回结果
  process(data) {
    for (let i = 0; i < data.length; i++) {
      let date = wx.formatTime(new Date(parseInt(data[i].gmtcreate) * 1e3), 'd-t').split(' '),
        _txt = data[i].description;  // cut the excess words after 3 lines
      data[i].time = date[0] == wx.formatTime(new Date()) ? date[1] : date[0];
      data[i].text = _txt.length > 40 ? _txt.substring(0, 40) + '...' : data[i].text = _txt;
      data[i].endTime = wx.formatTime(new Date(parseInt(data[i].gmtend) * 1e3), '/');
      data[i].cardId = i;
    }
    return data;
  },

  onShow() {
    this.setData({ loading: true });
    wx.onShow(this, text[wx.lang].title);
    let array = [], post = {
      idUser: wx.appData.user.iduser,
      tab: this.data.showWhat
    }
    // navigated back from submit page
    if (wx.appData.refreshTab) {
      post.tab = wx.appData.refreshTab
    }
    for (let i = 0; i < 6; ++i) array.push('card[' + i + ']');
    wx.post('Practice.getOngoingList', post, res => {
      let data = res.success ? this.process(res.data) : [];

      if (wx.appData.refreshTab || this.data.showWhat) 
        this.setData({ P1: data });
      else 
        this.setData({ P0: data });


      if (!data.length) {
        this.setData({ loading: false });
        return;
      }  // stop processing when no data exists
      wx._slide_v(this, array, wx.system.height * .8, 0, () => {
        this.setData({ loading: false });
        wx.slide_v(this, array, 0, 1, 0);
      });
    }, text[wx.lang].load);

  },

  changeTabs(e) {
    this.setData({ loading: true });
    // target = !index-- ? this.data.P0 : !index ? this.data.P1 : this.data.P2;
    var target = this.data[`P${e.detail.index}`];
    if (!target.length) {
      wx.post('Practice.getOngoingList', { idUser: wx.appData.user.iduser, tab: e.detail.index }, res => {
        let data = res.success ? this.process(res.data) : [];
        switch (e.detail.index) {
          case 0: this.setData({ P0: data });
            break;
          case 1: this.setData({ P1: data });
            break;
          case 2: this.setData({ P2: data });
            break;
        }
        if (!data.length) {
          this.setData({ loading: false });
          return;
        };  // stop processing when no data exists
        let array = [];
        for (let i = 0; i < 6; ++i) array.push('card[' + i + ']');
        wx._slide_v(this, array, wx.system.height * .8, 0, () => {
          this.setData({ loading: false });
          wx.slide_v(this, array, 0, 1, 0);
        });
      }, text[wx.lang].load);
    }
  },

  onReachBottom() {
    // 还没做
  },


  add() {
    wx.onHide(this, '../release/release');
  },

})